import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";

var _dec, _dec2, _dec3, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _dec10, _dec11, _dec12, _dec13, _class, _class2;

/**
 * Purcat - 租户级权限维护tab页 - 采购品类
 * @date: 2018-7-31

 */
import React from 'react';
import { connect } from 'dva';
import { Form, Input, Button, Modal, Tooltip, Switch, Table } from 'antd';
import { Bind } from 'lodash-decorators';
import intl from 'utils/intl';
import { CPAAS_IAM } from 'utils/config';
import notification from 'utils/notification';
import { tableScrollWidth, getCurrentOrganizationId } from 'utils/utils';
import AddDataModal from "../../../../../components/AuthorityManagement/AddDataModal";
/**
 * 使用 Form.Item 组件
 */

var FormItem = Form.Item;
/**
 * 租户级权限管理 - 采购品类
 * @extends {Component} - React.Component
 * @reactProps {Object} authorityPurcat - 数据源
 * @reactProps {Object} loading - 数据加载是否完成
 * @reactProps {Object} form - 表单对象
 * @reactProps {Function} [dispatch=function(e) {return e;}] - redux dispatch方法
 * @return React.element
 */

var Purcat = (_dec = Form.create({
  fieldNameProp: null
}), _dec2 = connect(function (_ref) {
  var authorityPurcat = _ref.authorityPurcat,
      loading = _ref.loading;
  return {
    authorityPurcat: authorityPurcat,
    organizationId: getCurrentOrganizationId(),
    addLoading: loading.effects['authorityPurcat/addAuthorityPurcat'],
    fetchLoading: loading.effects['authorityPurcat/fetchAuthorityPurcat']
  };
}), _dec3 = Bind(), _dec4 = Bind(), _dec5 = Bind(), _dec6 = Bind(), _dec7 = Bind(), _dec8 = Bind(), _dec9 = Bind(), _dec10 = Bind(), _dec11 = Bind(), _dec12 = Bind(), _dec13 = Bind(), _dec(_class = _dec2(_class = (_class2 = /*#__PURE__*/function (_React$Component) {
  _inherits(Purcat, _React$Component);

  var _super = _createSuper(Purcat);

  /**
   *Creates an instance of Purcat.
   * @param {Object} props 属性
   */
  function Purcat(props) {
    var _this;

    _classCallCheck(this, Purcat);

    _this = _super.call(this, props);
    _this.state = {
      selectRows: [],
      switchLoading: false,
      addModalVisible: false
    };
    return _this;
  }

  _createClass(Purcat, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.fetchData();
    }
    /**
     *查询数据
     *
     * @param {*Object} pageData
     */

  }, {
    key: "fetchData",
    value: function fetchData() {
      var pageData = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      var _this$props = this.props,
          form = _this$props.form,
          dispatch = _this$props.dispatch,
          userId = _this$props.userId;
      var staticData = {
        userId: userId,
        authorityTypeCode: 'PURCAT'
      };
      form.validateFields(function (err, fieldsValue) {
        if (!err) {
          dispatch({
            type: 'authorityPurcat/fetchAuthorityPurcat',
            payload: _objectSpread(_objectSpread(_objectSpread({}, fieldsValue), staticData), pageData)
          });
        }
      });
    }
    /**
     * 添加数据
     * @param {Aarray} addRows 选择的数据
     */

  }, {
    key: "addPurcat",
    value: function addPurcat(addRows) {
      var _this2 = this;

      var _this$props2 = this.props,
          dispatch = _this$props2.dispatch,
          _this$props2$authorit = _this$props2.authorityPurcat.head,
          head = _this$props2$authorit === void 0 ? {} : _this$props2$authorit,
          userId = _this$props2.userId;
      dispatch({
        type: 'authorityPurcat/addAuthorityPurcat',
        payload: {
          authorityTypeCode: 'PURCAT',
          userId: userId,
          userAuthority: head,
          userAuthorityLineList: addRows
        }
      }).then(function (response) {
        if (response) {
          _this2.onHideAddModal();

          notification.success();

          _this2.refresh();
        }
      });
    }
    /**
     *删除方法
     */

  }, {
    key: "remove",
    value: function remove() {
      var _this3 = this;

      var _this$props3 = this.props,
          dispatch = _this$props3.dispatch,
          userId = _this$props3.userId;
      var selectRows = this.state.selectRows;

      var onOk = function onOk() {
        dispatch({
          type: 'authorityPurcat/deleteAuthorityPurcat',
          payload: {
            userId: userId,
            deleteRows: selectRows
          }
        }).then(function (response) {
          if (response) {
            _this3.refresh();

            notification.success();
          }
        });
      };

      Modal.confirm({
        title: intl.get('cpaas.common.message.confirm.remove').d('确定删除选中数据？'),
        onOk: onOk
      });
    }
    /**
     *刷新
     */

  }, {
    key: "refresh",
    value: function refresh() {
      this.fetchData();
      this.setState({
        selectRows: []
      });
    }
    /**
     * 表格勾选
     * @param {null} _ 占位
     * @param {object} selectedRow 选中行
     */

  }, {
    key: "onSelectChange",
    value: function onSelectChange(_, selectedRows) {
      this.setState({
        selectRows: selectedRows
      });
    }
    /**
     * 展示弹出框
     */

  }, {
    key: "onShowAddModal",
    value: function onShowAddModal() {
      this.setState({
        addModalVisible: true
      });
    }
    /**
     * 隐藏弹出框
     */

  }, {
    key: "onHideAddModal",
    value: function onHideAddModal() {
      this.setState({
        addModalVisible: false
      });
    }
    /**
     *点击查询按钮事件
     */

  }, {
    key: "queryValue",
    value: function queryValue() {
      this.fetchData();
      this.setState({
        selectRows: []
      });
    }
    /**
     *分页change事件
     */

  }, {
    key: "handleTableChange",
    value: function handleTableChange() {
      var pagination = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      this.fetchData({
        page: pagination
      });
    }
    /**
     * 表单重置
     */

  }, {
    key: "handleFormReset",
    value: function handleFormReset() {
      var form = this.props.form;
      form.resetFields();
    }
    /**
     *点击加入全部后触发事件
     *
     * @param {Boolean} checked switch的value值
     */

  }, {
    key: "includeAllFlag",
    value: function includeAllFlag(checked) {
      var _this4 = this;

      var _this$props4 = this.props,
          dispatch = _this$props4.dispatch,
          userId = _this$props4.userId,
          _this$props4$authorit = _this$props4.authorityPurcat.head,
          head = _this$props4$authorit === void 0 ? {} : _this$props4$authorit;
      this.setState({
        switchLoading: true
      });
      dispatch({
        type: 'authorityPurcat/addAuthorityPurcat',
        payload: {
          authorityTypeCode: 'PURCAT',
          userId: userId,
          userAuthority: _objectSpread(_objectSpread({}, head), {}, {
            includeAllFlag: checked ? 1 : 0
          }),
          userAuthorityLineList: []
        }
      }).then(function (response) {
        if (response) {
          _this4.refresh();

          notification.success();

          _this4.setState({
            switchLoading: false
          });
        }
      });
    }
    /**
     *渲染查询结构
     *
     * @returns
     */

  }, {
    key: "renderForm",
    value: function renderForm() {
      var _this5 = this;

      var getFieldDecorator = this.props.form.getFieldDecorator;
      return /*#__PURE__*/React.createElement(Form, {
        layout: "inline"
      }, /*#__PURE__*/React.createElement(FormItem, {
        label: intl.get('ciam.authority.model.authorityPurcat.dataName').d('品类名称')
      }, getFieldDecorator('dataName')( /*#__PURE__*/React.createElement(Input, null))), /*#__PURE__*/React.createElement(FormItem, {
        label: intl.get('ciam.authority.model.authorityPurcat.dataCode').d('品类代码')
      }, getFieldDecorator('dataCode')( /*#__PURE__*/React.createElement(Input, {
        typeCase: "upper",
        trim: true,
        inputChinese: false
      }))), /*#__PURE__*/React.createElement(FormItem, null, /*#__PURE__*/React.createElement(Button, {
        style: {
          marginRight: 8
        },
        onClick: this.handleFormReset
      }, intl.get('cpaas.common.button.reset').d('重置')), /*#__PURE__*/React.createElement(Button, {
        type: "primary",
        onClick: function onClick() {
          return _this5.queryValue();
        },
        htmlType: "submit"
      }, intl.get('cpaas.common.button.search').d('查询'))));
    }
    /**
     *渲染事件
     *
     * @returns
     */

  }, {
    key: "render",
    value: function render() {
      var _this6 = this;

      var _this$props5 = this.props,
          _this$props5$authorit = _this$props5.authorityPurcat,
          _this$props5$authorit2 = _this$props5$authorit.list,
          list = _this$props5$authorit2 === void 0 ? [] : _this$props5$authorit2,
          _this$props5$authorit3 = _this$props5$authorit.head,
          head = _this$props5$authorit3 === void 0 ? {} : _this$props5$authorit3,
          _this$props5$authorit4 = _this$props5$authorit.pagination,
          pagination = _this$props5$authorit4 === void 0 ? {} : _this$props5$authorit4,
          userId = _this$props5.userId,
          organizationId = _this$props5.organizationId,
          addLoading = _this$props5.addLoading,
          fetchLoading = _this$props5.fetchLoading;
      var _this$state = this.state,
          switchLoading = _this$state.switchLoading,
          addModalVisible = _this$state.addModalVisible,
          selectRows = _this$state.selectRows;
      var columns = [{
        title: intl.get('ciam.authority.model.authorityPurcat.dataName').d('品类名称'),
        dataIndex: 'dataName'
      }, {
        title: intl.get('ciam.authority.model.authorityPurcat.dataCode').d('品类代码'),
        dataIndex: 'dataCode',
        width: 300
      }];
      var addModalOptions = {
        defaultFlag: true,
        rowKey: 'dataId',
        title: intl.get('ciam.authority.view.title.modal.purcat').d('选择采购品类'),
        lov: {
          lovTypeCode: 'URL',
          valueField: 'dataId',
          displayField: 'dataName',
          tableFields: columns,
          queryFields: [{
            label: intl.get('ciam.authority.model.authorityPurcat.dataCode').d('品类代码'),
            field: 'dataCode'
          }, {
            label: intl.get('ciam.authority.model.authorityPurcat.dataName').d('品类名称'),
            field: 'dataName'
          }],
          queryUrl: "".concat(CPAAS_IAM, "/v1/").concat(organizationId, "/users/").concat(userId, "/data/purcats")
        },
        confirmLoading: addLoading,
        modalVisible: addModalVisible,
        onHideAddModal: this.onHideAddModal,
        addData: this.addPurcat
      };
      var rowSelection = {
        onChange: this.onSelectChange,
        selectedRowKeys: selectRows.map(function (n) {
          return n.authorityLineId;
        })
      };
      return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("div", {
        className: "table-list-search"
      }, this.renderForm()), /*#__PURE__*/React.createElement("div", {
        style: {
          textAlign: 'right'
        }
      }, !head.includeAllFlag && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Button, {
        style: {
          margin: '0 8px 16px 0'
        },
        onClick: function onClick() {
          return _this6.onShowAddModal();
        }
      }, intl.get('ciam.authority.view.button.table.create.purcat').d('新建采购品类权限')), /*#__PURE__*/React.createElement(Button, {
        style: {
          margin: '0 8px 16px 0'
        },
        disabled: selectRows.length <= 0,
        onClick: function onClick() {
          return _this6.remove();
        }
      }, intl.get('ciam.authority.view.button.table.delete.purcat').d('删除采购品类权限'))), /*#__PURE__*/React.createElement("div", {
        style: {
          display: 'inline-block',
          margin: '0 8px 16px 0'
        }
      }, /*#__PURE__*/React.createElement("span", {
        style: {
          marginRight: '8px'
        }
      }, intl.get('ciam.authority.view.message.label').d('加入全部:')), /*#__PURE__*/React.createElement(Tooltip, {
        title: intl.get('ciam.authority.view.message.title.tooltip.pc').d('“加入全部”即将所有品类权限自动添加至当前账户，无需再手工添加。'),
        placement: "right"
      }, /*#__PURE__*/React.createElement(Switch, {
        loading: switchLoading || fetchLoading,
        checked: !!head.includeAllFlag,
        onChange: this.includeAllFlag
      })))), /*#__PURE__*/React.createElement(Table, {
        bordered: true,
        rowKey: "authorityLineId",
        loading: fetchLoading,
        dataSource: list,
        columns: columns,
        rowSelection: rowSelection,
        pagination: pagination,
        scroll: {
          x: tableScrollWidth(columns)
        },
        onChange: this.handleTableChange
      }), /*#__PURE__*/React.createElement(AddDataModal, addModalOptions));
    }
  }]);

  return Purcat;
}(React.Component), (_applyDecoratedDescriptor(_class2.prototype, "fetchData", [_dec3], Object.getOwnPropertyDescriptor(_class2.prototype, "fetchData"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "addPurcat", [_dec4], Object.getOwnPropertyDescriptor(_class2.prototype, "addPurcat"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "remove", [_dec5], Object.getOwnPropertyDescriptor(_class2.prototype, "remove"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "refresh", [_dec6], Object.getOwnPropertyDescriptor(_class2.prototype, "refresh"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "onSelectChange", [_dec7], Object.getOwnPropertyDescriptor(_class2.prototype, "onSelectChange"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "onShowAddModal", [_dec8], Object.getOwnPropertyDescriptor(_class2.prototype, "onShowAddModal"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "onHideAddModal", [_dec9], Object.getOwnPropertyDescriptor(_class2.prototype, "onHideAddModal"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "queryValue", [_dec10], Object.getOwnPropertyDescriptor(_class2.prototype, "queryValue"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleTableChange", [_dec11], Object.getOwnPropertyDescriptor(_class2.prototype, "handleTableChange"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleFormReset", [_dec12], Object.getOwnPropertyDescriptor(_class2.prototype, "handleFormReset"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "includeAllFlag", [_dec13], Object.getOwnPropertyDescriptor(_class2.prototype, "includeAllFlag"), _class2.prototype)), _class2)) || _class) || _class);
export { Purcat as default };